package org.nfyszny.zzzs.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.system.vo.LoginUser;
import org.nfyszny.zzzs.modules.constant.CertificateConstant;
import org.nfyszny.zzzs.modules.entity.ZzzsCertificate;
import org.nfyszny.zzzs.modules.mapper.ZzzsCertificateMapper;
import org.nfyszny.zzzs.modules.service.IZzzsCertificateService;
import org.nfyszny.zzzs.modules.utils.UserUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 证书表
 * @Author: jeecg-boot
 * @Date: 2022-11-11
 * @Version: V1.0
 */
@Service
public class ZzzsCertificateServiceImpl extends ServiceImpl<ZzzsCertificateMapper, ZzzsCertificate> implements IZzzsCertificateService {
    /**
     * 修改状态为作废
     * @param zzzsCertificate
     */
    @Override
    public void cancellationCertificate(ZzzsCertificate zzzsCertificate) {
        zzzsCertificate = baseMapper.selectById(zzzsCertificate.getId());
        System.out.println("zzzsCertificate = " + zzzsCertificate);
        zzzsCertificate.setStatus(CertificateConstant.CertificateStatus.OVERDUE.getCode());
        baseMapper.updateById(zzzsCertificate);
    }

    /**
     * 修改状态
     * @param zzzsCertificate
     */
    @Override
    public void suspend(ZzzsCertificate zzzsCertificate) {
        zzzsCertificate = baseMapper.selectById(zzzsCertificate.getId());
        System.out.println("zzzsCertificate = " + zzzsCertificate);
        zzzsCertificate.setStatus(CertificateConstant.CertificateStatus.INVALID.getCode());
        baseMapper.updateById(zzzsCertificate);
    }

    @Override
    public void createCertificate() {

    }

    @Override
    public IPage<ZzzsCertificate> setListByStatus(Page<ZzzsCertificate> page, QueryWrapper<ZzzsCertificate> queryWrapper, int... code) {
        for (int i : code) {
            queryWrapper.eq("status", code);
        }

        return this.page(page, queryWrapper);
    }

    @Override
    public Boolean getStatus(String id, Boolean is) {
        LoginUser userInfo = UserUtils.getUserInfo();
        Boolean result = false;
        //标识为如果为false则查询公司的备案状态
        QueryWrapper<ZzzsCertificate> queryWrapper = new QueryWrapper<>();
        if (is) {
            queryWrapper.eq("sys_org_code", userInfo.getOrgCode());
            queryWrapper.eq("certificate_type", CertificateConstant.CertificateType.ENTERPRISE_RECORD.getCode());

        } else {
            queryWrapper.eq("certificate_id", id);

        }
        List<ZzzsCertificate> list = baseMapper.selectList(queryWrapper);
        System.out.println("list=>"+list);
        if (list.size() != 0) {
            for (ZzzsCertificate certificate : list) {
                if (certificate.getStatus() == CertificateConstant.CertificateStatus.GIVEN_OUT.getCode()) {
                    result = true;
                    break;
                }
            }
        }
        return result;
    }


    @Override
    public List<ZzzsCertificate> getCertificate(String certificateId) {
        QueryWrapper<ZzzsCertificate> queryWrapper = new QueryWrapper<>();
        Map<String, Object> map = new HashMap<>();
        queryWrapper.eq("certificate_id", certificateId);
        queryWrapper.eq("status", CertificateConstant.CertificateStatus.GIVEN_OUT.getCode()).or().eq("status", CertificateConstant.CertificateStatus.INVALID.getCode());
        List<ZzzsCertificate> list = baseMapper.selectList(queryWrapper);
        return list;
    }




}
